﻿using System;
using System.Collections.Generic;
using System.Reflection;

namespace Com.OpenCode.Framework.Core
{
    /// <summary>
    ///     运行时环境类。
    /// </summary>
    public class Runtime
    {
        private List<Assembly> _Assemblys = new List<Assembly>();

        /// <summary>
        ///     运行时环境包含程序集的数量。
        /// </summary>
        public virtual int Count
        {
            get { return _Assemblys.Count; }
        }

        /// <summary>
        ///     移除运行时中包含的程序集。
        /// </summary>
        public virtual void Clear()
        {
            _Assemblys.Clear();
        }

        /// <summary>
        ///     根据索引获取相应的程序集。
        /// </summary>
        /// <param name="inde">程序集索引。</param>
        /// <returns>返回的程序集。</returns>
        public virtual Assembly Get(int index)
        {
            return _Assemblys[index];
        }

        /// <summary>
        ///     根据索引删除对应程序集。
        /// </summary>
        /// <param name="index">程序集索引。</param>
        public virtual void Delete(int index)
        {
            _Assemblys.RemoveAt(index);
        }

        /// <summary>
        ///     根据程序集名称删除对应的程序集。
        /// </summary>
        /// <param name="assemblyName">需要移除的程序集名称。</param>
        /// <returns></returns>
        public void Delete(string assemblyName)
        {
            _Assemblys.RemoveAll(ass => ass.ManifestModule.Name == assemblyName);
        }

        /// <summary>
        ///     根据程序集名称获取程序集。
        /// </summary>
        /// <param name="assemblyName">程序集名称,例如。Com.OpenCode.Framework.Application.dll</param>
        /// <returns></returns>
        public virtual Assembly Get(string assemblyName)
        {
            return _Assemblys.Find(ass => ass.ManifestModule.Name == assemblyName);
        }

        /// <summary>
        ///     添加程序集。
        /// </summary>
        /// <param name="assembly">需要添加的程序集。</param>
        public virtual void Add(Assembly assembly)
        {
            if (Contains(assembly.ManifestModule.Name))
            {
                Delete(assembly.ManifestModule.Name);
            }
            _Assemblys.Add(assembly);
        }

        /// <summary>
        ///     根据程序集所在位置加载程序集。
        /// </summary>
        /// <param name="assemblyFile">加载的程序集路径。</param>
        public virtual void Add(string assemblyFile)
        {
            Assembly _Assembly = Assembly.LoadFrom(assemblyFile);
            Add(_Assembly);
        }

        /// <summary>
        ///     指定运行环境中是否包含指定名称的程序集。
        /// </summary>
        /// <param name="assemblyName">需要检查的程序集名称。</param>
        /// <returns></returns>
        public bool Contains(string assemblyName)
        {
            return _Assemblys.Find(ass => ass.ManifestModule.Name == assemblyName) != null;
        }

        /// <summary>
        ///     创建对象。
        /// </summary>
        /// <param name="className"></param>
        /// <returns></returns>
        public virtual object CreateObject(string className)
        {
            object obj = null;
            if (string.IsNullOrEmpty(className))
            {
                LogServices.WriteError(string.Format("对象名称不能为空。"), null, false);
                return null;
            }
            foreach (Assembly assembly in _Assemblys)
            {
                try
                {
                    obj = assembly.CreateInstance(className);
                    if (obj != null)
                    {
                        break;
                    }
                }
                catch (Exception ex)
                {
                    LogServices.WriteError(string.Format("创建对象失败{0},{1}", className, ex.Message),null,false);
                }
            }
            if (obj==null)
            {
                LogServices.WriteError(string.Format("没有找到类型为{0}的对象。", className), null, false);
            }
            return obj;
        }
    }
}